<?php

namespace Hilaoyu\LaravelExtend\Services\Variables;



use Hilaoyu\LaravelExtend\Model;
use Hilaoyu\LaravelExtend\Services\Files\Traits\ModelWithFilesTrait;

abstract class VariablesModelAbstract extends Model
{
    protected $table = 'variables';
    protected $file_save_disk = '';
    protected $image_save_disk = '';
    protected $content_file_save_disk = '';


    protected $fillable = [
        'name',
        'key',
        'value',
        'remark',
        'type',
        'is_mult',
        'required',
    ];

    protected $casts = [
      'value'=>'json',
      'required'=>'int',
    ];


    public function usesTimestamps()
    {
        return false;
    }

    /*
     * 验证规则
     * */
    public function getValidationRules()
    {
        $rules =  [
            'key' => 'required|string|max:100|unique:'. static::getTableCon() .',key,' . $this->getOriginalKey() . ',' . static::getPkName(),
        ];

        return $rules;
    }

    public function getValidationMessages()
    {
        return [
            'key.required' => '键名必填',
            'key.string' => '键名必须是字符串',
            'key.max' => '键名太长了',
            'key.unique' => '键名已存在',
        ];
    }

    public function isFile(){
        return 'file' == data_get($this->attributes,'type');
    }
    public function isImage(){

        return 'image' == data_get($this->attributes,'type');
    }
    public function isRichText(){

        return 'textrich' == data_get($this->attributes,'type');
    }

    /*public function getCasts()
    {
        $cats = parent::getCasts();

        if($this->isRichText()){
            unset($cats['value']);
        }

        return $cats;
    }*/


    use ModelWithFilesTrait;
    protected function modelWithFilesTraitSettingFileFields(): array
    {
        if($this->file_save_disk && $this->isFile()){
            return [
                'value' => $this->file_save_disk
            ];
        }

        return [];
    }

    protected function modelWithFilesTraitSettingImageFields(): array
    {
        if($this->file_save_disk && $this->isImage()){
            return [
                'value' => $this->file_save_disk
            ];
        }

        return [];
    }

    protected function modelWithFilesTraitSettingContentHasFileFields(): array
    {
        if($this->file_save_disk && $this->isRichText()){
            return [
                'value' => $this->file_save_disk
            ];
        }

        return [];
    }
}
